<?php
include("controller.php");
include(MODELDIR."admin.php");
class LoginController  extends Controller
{
	public function indexAction()
	{
		@$_SESSION['tag'] +=1 ;
		if (empty($_SERVER['PHP_AUTH_USER'])||empty($_SESSION['auth']))
		{
			header('WWW-Authenticate: Basic realm="www.repairall.pt"');
			header('HTTP/1.0 401 Unauthorized');
			$_SESSION['auth']='auth';
			exit;
		}
		else
		{
			session_unregister("auth");
			$uname = $_SERVER['PHP_AUTH_USER'];
			$uname = mysql_escape_string($uname);
			$pword = $_SERVER['PHP_AUTH_PW'];
			$admin=new Admin();
			if($admin->login($uname,$pword))
			{
				echo "success";
			}
			else  if($_SESSION['tag']<=4)
			{
				header("Location:".BASEURL.$this->getModuleName()."/".$this->getControllerName()."/".$this->getActionName());
			}
			else
			{
				header("Location:".BASEURL);
			}
		}
	}

	public function logoutAction()
	{
		$_SESSION = array();
		if (isset($_COOKIE[session_name()])) {
			setcookie(session_name(), '', time()-42000, '/');
		}
		session_destroy();
		echo '<script>location.href="',BASEURL,'";</script>';
	}
}